/**
 * 
 * @author: HXJ
 * @date: 2021/04/25 21:47:23
 */
export default class wxPackage {
    private static packageSub: Array<any>
    static wxPackage() {
        throw new Error("Method not implemented.")
    }

    static async asyncLoad(packages,caller?,callBack?) {
        this.packageSub = packages;
        return await this.loadPackage(caller,callBack);
    }
    static async loadPackage(caller,callBack:Function) {
        if (Laya.Browser.onWeiXin) {
            let i=0;
            for (let k in this.packageSub) {
                i++;
                let type;
                let data = await this.onloadPackage(this.packageSub[k]) as any
                if (data.errMsg == "loadSubpackage:ok") {
                    // type = ""
                    console.log("分包加载成功", this.packageSub[k])
                } else {
                    console.error("分包加载失败", this.packageSub[k])
                }
                callBack&&callBack.call(caller,...[i/this.packageSub.length,this.packageSub[k],type])
            }
        }
        return true
    }
    static async onloadPackage(packageName) {
        return await new Promise((res, rej) => {
            wx['loadSubpackage']({
                name: packageName, // name 可以填 name 或者 root
                success: res,
                fail: res
            })
        })
    }

}